Merge branch 'master' of ssh://xp-dev.com/RRRRHHHH_Code
[RRRRHHHH_Code] / ruralHouses client / src / gui / HouseFeaturesGUI.java
1 package gui;
2
3 import java.awt.Color;
4 import java.awt.Component;
5 import java.awt.event.ActionEvent;
6 import java.awt.event.ActionListener;
7 import java.awt.event.MouseAdapter;
8 import java.awt.event.MouseEvent;
9 import java.rmi.Naming;
10 import java.util.Date;
11 import java.util.Enumeration;
12 import java.util.Vector;
13
14 import javax.swing.JButton;
15 import javax.swing.JFrame;
16 import javax.swing.JLabel;
17 import javax.swing.JPanel;
18 import javax.swing.JScrollPane;
19 import javax.swing.JTable;
20 import javax.swing.JTextField;
21 import javax.swing.SwingConstants;
22 import javax.swing.border.EmptyBorder;
23 import javax.swing.table.DefaultTableCellRenderer;
24 import javax.swing.table.DefaultTableModel;
25
26 import common.BookingInterface;
27
28 import configuration.___IntNames;
29 import domain.Booking;
30 import domain.Client;
31 import domain.Offer;
32 import domain.RuralHouse;
33
34 public class HouseFeaturesGUI extends JFrame {
35
36         /**
37          * 
38          */
39         private static final long serialVersionUID = 1L;
40         private JPanel contentPane;
41         private JLabel lblDistrict;
42         private JTextField District_f;
43         private JLabel lblDescription;
44         private JTextField description_f;
45         private JLabel lblKitchen;
46         private JTextField kitchens_f;
47         private JLabel lblRooms;
48         private JTextField rooms_f;
49         private JLabel lblLivings;
50         private JTextField lRooms_f;
51         private JLabel lblParkings;
52         private JTextField parkings_f;
53         private JLabel lblBaths;
54         private JTextField baths_f;
55         private JTable table;
56         private DefaultTableModel tableModel;
57         private RuralHouse rh;
58         private JTextField telIn;
59         private int row;
60         private JLabel labelPhone;
61         private JLabel lblName;
62         private JTextField nameField;
63         private JLabel lblEmail;
64         private JTextField mailField;
65
66         /**
67          * Create the frame.
68          */
69
70         public HouseFeaturesGUI(RuralHouse RH, final Date FirstDay,
71                         final Date LastDay) {
72                 this.rh = RH;
73                 this.getContentPane().setLayout(null);
74                 setBounds(100, 100, 500, 700);
75                 contentPane = new JPanel();
76                 contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
77                 setContentPane(contentPane);
78
79                 lblDistrict = new JLabel("District:");
80                 lblDistrict.setBounds(23, 67, 70, 14);
81                 lblDistrict.setHorizontalAlignment(SwingConstants.RIGHT);
82
83                 District_f = new JTextField();
84                 District_f.setEditable(false);
85                 District_f.setBounds(103, 64, 86, 20);
86                 District_f.setColumns(10);
87
88                 lblDescription = new JLabel("Description:");
89                 lblDescription.setBounds(215, 67, 90, 14);
90                 lblDescription.setHorizontalAlignment(SwingConstants.RIGHT);
91
92                 description_f = new JTextField();
93                 description_f.setEditable(false);
94                 description_f.setBounds(238, 99, 178, 129);
95                 description_f.setColumns(10);
96
97                 lblKitchen = new JLabel("Kitchens:");
98                 lblKitchen.setBounds(23, 211, 70, 14);
99                 lblKitchen.setHorizontalAlignment(SwingConstants.RIGHT);
100
101                 kitchens_f = new JTextField();
102                 kitchens_f.setEditable(false);
103                 kitchens_f.setBounds(103, 202, 86, 20);
104                 kitchens_f.setColumns(10);
105
106                 lblRooms = new JLabel("Rooms:");
107                 lblRooms.setBounds(23, 102, 70, 14);
108                 lblRooms.setHorizontalAlignment(SwingConstants.RIGHT);
109
110                 rooms_f = new JTextField();
111                 rooms_f.setEditable(false);
112                 rooms_f.setBounds(103, 99, 86, 20);
113                 rooms_f.setColumns(10);
114
115                 lblLivings = new JLabel("Living rooms:");
116                 lblLivings.setBounds(30, 236, 63, 14);
117                 lblLivings.setHorizontalAlignment(SwingConstants.RIGHT);
118
119                 lRooms_f = new JTextField();
120                 lRooms_f.setEditable(false);
121                 lRooms_f.setBounds(103, 233, 86, 20);
122                 lRooms_f.setColumns(10);
123
124                 lblParkings = new JLabel("Parkings:");
125                 lblParkings.setBounds(23, 174, 70, 14);
126                 lblParkings.setHorizontalAlignment(SwingConstants.RIGHT);
127
128                 parkings_f = new JTextField();
129                 parkings_f.setEditable(false);
130                 parkings_f.setBounds(103, 171, 86, 20);
131                 parkings_f.setColumns(10);
132
133                 lblBaths = new JLabel("Baths:");
134                 lblBaths.setBounds(23, 143, 70, 14);
135                 lblBaths.setHorizontalAlignment(SwingConstants.RIGHT);
136
137                 baths_f = new JTextField();
138                 baths_f.setEditable(false);
139                 baths_f.setBounds(103, 140, 86, 20);
140                 baths_f.setColumns(10);
141                 contentPane.setLayout(null);
142                 contentPane.add(lblParkings);
143                 contentPane.add(parkings_f);
144                 contentPane.add(lblRooms);
145                 contentPane.add(rooms_f);
146                 contentPane.add(lblBaths);
147                 contentPane.add(baths_f);
148                 contentPane.add(lblDistrict);
149                 contentPane.add(District_f);
150                 contentPane.add(lblLivings);
151                 contentPane.add(lRooms_f);
152                 contentPane.add(lblKitchen);
153                 contentPane.add(kitchens_f);
154                 contentPane.add(description_f);
155                 contentPane.add(lblDescription);
156                 parkings_f.setText(Integer.toString(rh.getFeatures().getnParkings()));
157                 rooms_f.setText(Integer.toString(rh.getFeatures().getnRooms()));
158                 baths_f.setText(Integer.toString(rh.getFeatures().getnBaths()));
159                 District_f.setText(rh.getDistrict());
160                 kitchens_f.setText(Integer.toString(rh.getFeatures().getnKitchens()));
161                 description_f.setText(rh.getDescription());
162                 lRooms_f.setText(Integer.toString(rh.getFeatures().getnLivings()));
163                 JLabel lblAvailbleOffers = new JLabel("Availble Offers:");
164                 lblAvailbleOffers.setBounds(30, 286, 86, 14);
165                 contentPane.add(lblAvailbleOffers);
166                 JScrollPane scrollPane = new JScrollPane();
167                 scrollPane.setBounds(40, 311, 376, 183);
168                 contentPane.add(scrollPane);
169
170                 table = new JTable() {
171                         private static final long serialVersionUID = 1L;
172
173                         public boolean isCellEditable(int row, int column) {
174                                 return false;
175                         };
176                 };
177                 tableModel = new DefaultTableModel(null, new String[] { "Offer #",
178                                 "FirstDay", "LastDay", "Price" });
179                 table.setModel(tableModel);
180                 scrollPane.setViewportView(table);
181
182                 JButton btnBookSelected = new JButton("Book SelectedOffer");
183                 btnBookSelected.setBounds(238, 614, 178, 23);
184                 contentPane.add(btnBookSelected);
185
186                 btnBookSelected.addActionListener(new ActionListener() {
187                         public void actionPerformed(ActionEvent arg0) {
188
189                                 jButton_ActionPerformed(arg0);
190                         }
191
192                 });
193
194                 JLabel lblNewLabel = new JLabel(
195                                 "Green: Suit your dates. Red: Do not suit your dates");
196                 lblNewLabel.setEnabled(false);
197                 lblNewLabel.setBounds(170, 282, 261, 23);
198                 contentPane.add(lblNewLabel);
199
200                 JLabel lblNewLabel_1 = new JLabel("Telephone num:");
201                 lblNewLabel_1.setBounds(10, 618, 83, 14);
202                 contentPane.add(lblNewLabel_1);
203
204                 telIn = new JTextField();
205                 telIn.setBounds(103, 615, 129, 20);
206                 contentPane.add(telIn);
207                 telIn.setColumns(10);
208
209                 labelPhone = new JLabel("");
210                 labelPhone.setBounds(238, 252, 178, 14);
211                 contentPane.add(labelPhone);
212
213                 lblName = new JLabel("Name:");
214                 lblName.setBounds(10, 516, 46, 14);
215                 contentPane.add(lblName);
216
217                 nameField = new JTextField();
218                 nameField.setBounds(103, 513, 178, 20);
219                 contentPane.add(nameField);
220                 nameField.setColumns(10);
221
222                 lblEmail = new JLabel("E-mail:");
223                 lblEmail.setBounds(10, 565, 46, 14);
224                 contentPane.add(lblEmail);
225
226                 mailField = new JTextField();
227                 mailField.setBounds(103, 562, 178, 20);
228                 contentPane.add(mailField);
229                 mailField.setColumns(10);
230
231                 table.addMouseListener(new MouseAdapter() {
232                         @Override
233                         public void mouseClicked(MouseEvent arg0) {
234                                 row = table.getSelectedRow();
235                         }
236                 });
237                 Enumeration<Offer> rhs = rh.getAllOffers().elements();
238                 while (rhs.hasMoreElements()) {
239                         Offer of = rhs.nextElement();
240                         if (of.getBookings() != null) {
241                                 Vector<Object> row = new Vector<Object>();
242                                 row.add(of.getOfferNumber());
243                                 row.add(of.getFirstDay());
244                                 row.add(of.getLastDay());
245                                 row.add(of.getPrice());
246                                 tableModel.addRow(row);
247                         }
248                 }
249                 table.setDefaultRenderer(Object.class, new DefaultTableCellRenderer() {
250                         /**
251                          * 
252                          */
253                         private static final long serialVersionUID = 1L;
254
255                         @Override
256                         public Component getTableCellRendererComponent(JTable table,
257                                         Object value, boolean isSelected, boolean hasFocus,
258                                         int row, int col) {
259
260                                 super.getTableCellRendererComponent(table, value, isSelected,
261                                                 hasFocus, row, col);
262
263                                 Date firstDay = (Date) table.getModel().getValueAt(row, 1);
264                                 Date lastDay = (Date) table.getModel().getValueAt(row, 2);
265                                 if (FirstDay != null && LastDay != null) {
266                                         if (LastDay.before(lastDay) || FirstDay.after(firstDay)) {
267                                                 setBackground(Color.RED);
268                                                 setForeground(Color.BLACK);
269                                         } else {
270                                                 setBackground(Color.GREEN);
271                                                 setForeground(Color.BLACK);
272                                         }
273                                 }
274                                 return this;
275                         }
276                 });
277         }
278
279         private void jButton_ActionPerformed(ActionEvent arg0) {
280
281                 BookingInterface bookingM = null;
282                 try {
283                         bookingM = (BookingInterface) Naming
284                                         .lookup(___IntNames.BookingManager);
285                 } catch (Exception e1) {
286                         System.out.println("Error accessing remote authentication: "
287                                         + e1.toString());
288                 }
289                 
290                 if (telIn.getText().matches("[976]\\d{2}[.\\- ]?\\d{3}[.\\- ]?\\d{3}")
291                                 && mailField
292                                 .getText()
293                                 .matches(
294                                                 "^[_A-Za-z0-9-\\+]+(\\.[_A-Za-z0-9-]+)*@[A-Za-z0-9-]+(\\.[A-Za-z0-9]+)*(\\.[A-Za-z]{2,})$")
295                 && !nameField.getText().isEmpty()) {
296                         labelPhone.setText("");
297                         Vector<Booking> book = null;
298                         try {
299                                 if (table.getRowCount() != 0) {
300                                         Client cl = new Client(nameField.getText(),
301                                                         mailField.getText(), telIn.getText());
302                                         book = bookingM.createBooking(rh, rh.offers.get(row)
303                                                         .getFirstDay(), rh.offers.get(row).getLastDay(),cl);
304                                 }
305                         } catch (Exception e) {
306                                 e.printStackTrace();
307                         }
308                         if (book != null) {
309                                 BookRuralHouseConfirmationWindow confirmWindow = new BookRuralHouseConfirmationWindow(
310                                                 book.lastElement());
311                                 confirmWindow.setVisible(true);
312                         }
313                 } else {
314                         labelPhone.setText("Bad formatted data.");
315
316                 }
317         }
318 }